//
//  ForumTableCell.m
//  Blank
//
//  Created by 周星宇 on 15/9/28.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ForumTableCell.h"
#import "ForumCollectionCell.h"
#import <UIImageView+WebCache.h>

@implementation ForumTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.forumArr = [NSMutableArray array];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(170 * Windth, 60 * Height);
        flowLayout.minimumInteritemSpacing = 10 * Height;
        flowLayout.sectionInset = UIEdgeInsetsMake(10 * Windth, 10 * Height, 10 * Windth, 10 * Height);
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 10 * Height, self.contentView.frame.size.width, self.contentView.frame.size.height) collectionViewLayout:flowLayout];
        [self addSubview: self.collectionView];
        
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        [self.collectionView registerClass:[ForumCollectionCell class] forCellWithReuseIdentifier:@"forum"];
        self.collectionView.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ForumCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"forum" forIndexPath:indexPath];
    [cell setNeedsLayout];
    NSDictionary *dic = [self.forumArr objectAtIndex:indexPath.row];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"icon"]] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    cell.titleLabel.text = [dic objectForKey:@"name"];
    cell.numLabel.text = [[dic objectForKey:@"threads"] stringByAppendingString:@"个主题"];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.forumArr.count;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame =  CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self.collectionView reloadData];
    if ([NightModel getColorModel]) {
        self.collectionView.backgroundColor = [UIColor grayColor];
        [self.collectionView reloadData];
    }
    else{
        self.collectionView.backgroundColor = [UIColor whiteColor];
         [self.collectionView reloadData];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    [self.sendDelegate sendUrl:[[self.forumArr objectAtIndex:indexPath.row] objectForKey:@"fid"] name:[[self.forumArr objectAtIndex:indexPath.row] objectForKey:@"name"]];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
